/*
 * ----------------------------------------------------------------------------
 * "THE BEER-WARE LICENSE" (Revision 42):
 * <rom.prevot@gmail.com> wrote this file. As long as you retain this notice you
 * can do whatever you want with this stuff. If we meet some day, and you think
 * this stuff is worth it, you can buy me a beer in return R. Prevot
 * ----------------------------------------------------------------------------
 */

/******************************************************************************\
 *          ____                _______    _____ _             _              *
 *         |  _ \              |__   __|  / ____| |           | |             *
 *         | |_) | ___  _ __ _ __ | | ___| (___ | |_ ___   ___| | __          *
 *         |  _ < / _ \| '__| '_ \| |/ _ \\___ \| __/ _ \ / __| |/ /          *
 *         | |_) | (_) | |  | | | | | (_) |___) | || (_) | (__|   <           *
 *         |____/ \___/|_|  |_| |_|_|\___/_____/ \__\___/ \___|_|\_\          *
 *                                                                            *
\******************************************************************************/

package app.models;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

/**
 *
 * @author Ghost
 */
@Entity
@Table(name = "lignesventes", catalog = "BornToStock", schema = "")
@NamedQueries({
    @NamedQuery(name = "Lignevente.findAll", query = "SELECT l FROM Lignevente l"),
    @NamedQuery(name = "Lignevente.findById", query = "SELECT l FROM Lignevente l WHERE l.id = :id"),
    @NamedQuery(name = "Lignevente.findByPxVente", query = "SELECT l FROM Lignevente l WHERE l.pxVente = :pxVente"),
    @NamedQuery(name = "Lignevente.findByQuantite", query = "SELECT l FROM Lignevente l WHERE l.quantite = :quantite")})
public class Lignevente implements Serializable, Cloneable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id", nullable = false)
    private Integer id;
    @Basic(optional = false)
    @Column(name = "pxVente", nullable = false)
    private double pxVente;
    @Basic(optional = false)
    @Column(name = "quantite", nullable = false)
    private int quantite;
    @JoinColumn(name = "produit_id", referencedColumnName = "id", nullable = false)
    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    private Reference reference;
    @JoinColumn(name = "vente_id", referencedColumnName = "id", nullable = false)
    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    private Vente vente;

    public Lignevente() {
    }

    public Lignevente(Integer id) {
        this.id = id;
    }

    public Lignevente(Integer id, double pxVente, int quantite) {
        this.id = id;
        this.pxVente = pxVente;
        this.quantite = quantite;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public double getPxVente() {
        return pxVente;
    }

    public void setPxVente(double pxVente) {
        this.pxVente = pxVente;
    }

    public int getQuantite() {
        return quantite;
    }

    public void setQuantite(int quantite) {
        this.quantite = quantite;
    }

    public Reference getReference() {
        return reference;
    }

    public void setReference(Reference reference) {
        this.reference = reference;
    }

    public Vente getVente() {
        return vente;
    }

    public void setVente(Vente vente) {
        this.vente = vente;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 59 * hash + (this.id != null ? this.id.hashCode() : 0);
        hash = 59 * hash + (int) (Double.doubleToLongBits(this.pxVente) ^ (Double.doubleToLongBits(this.pxVente) >>> 32));
        hash = 59 * hash + this.quantite;
        hash = 59 * hash + (this.reference != null ? this.reference.hashCode() : 0);
        hash = 59 * hash + (this.vente != null ? this.vente.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Lignevente)) {
            return false;
        }
        Lignevente other = (Lignevente) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    public float getTotal() {
        return (float) (quantite * pxVente);
    }

    @Override
    public String toString() {
        return "Lignevente{" + "id=" + id + ", pxVente=" + pxVente + ", quantite=" + quantite + ", reference=" + reference + ", vente=" + vente.hashCode() + '}';
    }

    public Lignevente clone() {
        try {
            return (Lignevente) super.clone();
        } catch (Exception e) {
            return null;
        }
    }

    public void cancel() {
        Reference ref = this.getReference();
        int qte = ref.getStock() + this.getQuantite();
        ref.setStock(qte);
        this.setVente(null);
    }
}
